package com.yuan.jeinksgitlab;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

/**
 * restTemplate 再次封装
 * Created by 袁恩光 on 2017/9/26.
 */
public class HttpClient {

    /**
     * form post 请求
     * 以form表单的方式请求
     * 可以通过后台@RequestParam 和 request.getParam 获取参数
     */
    public static String post(String url, Map<String, Object> param) {
        MultiValueMap<String, Object> postData = new LinkedMultiValueMap<>();
        postData.setAll(param);
        HttpEntity<MultiValueMap> entity = new HttpEntity<>(postData);
        return new RestTemplate().postForObject(url, entity, String.class);
    }

    /**
     * post 请求 接收方式
     * InputStream is = request.getInputStream();
     * BufferedReader bf = new BufferedReader(new InputStreamReader(is));
     * String line = "";
     * StringBuilder sb = new StringBuilder();
     * while ((line = bf.readLine())!=null){
     * sb.append(line);
     * }
     * System.out.println(sb.toString());
     * 或者直接通过  @RequestBody 获取
     */
    public static ResponseEntity postInputStream(String url, Object obj) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        String json = "";
        if (!(obj instanceof String)) {
            json = JSONObject.toJSONString(obj);
        }
        HttpEntity<String> entity = new HttpEntity<>(json, headers);
        ResponseEntity<String> responseEntity = restTemplate().postForEntity(url, entity, String.class);
       return responseEntity;

    }

    /**
     * get请求方式
     */
    public static String get(String Url, Map<String, Object> param) {
        StringBuilder sb = new StringBuilder();
        for (String key : param.keySet()) {
            sb.append("&");
            sb.append(key);
            sb.append("=");
            sb.append(param.get(key));
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(0);
        }
        URI uri = null;
        try {
            uri = new URI(Url + "?" + sb.toString());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return new RestTemplate().getForObject(uri, String.class);
    }

    public static RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setErrorHandler(new ResponseErrorHandler() {
            @Override
            public boolean hasError(ClientHttpResponse response) throws IOException {
                return false;
            }

            @Override
            public void handleError(ClientHttpResponse response) throws IOException {

            }
        });

        return restTemplate;
    }

}
